package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.GridImgAdapter;
import com.dlwx.firefighting.bean.AfterSaleBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AfterSaleDetailActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.goods_money_tv)
    TextView goodsMoneyTv;
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.order_num_tv)
    TextView orderNumTv;
    @BindView(R.id.status)
    TextView statusTv;
    private String userId;
    private GridImgAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_sale_detail);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        String id = getIntent().getStringExtra("id");
        initView();
        initRecy();
        getDatas(id);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("售后详情");
    }

    private void getDatas(String id) {
        OkGo.<String>post(Constant.AFTERSALE_INFO)
                .params("user_id", userId)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<AfterSaleBean> beans = dealResult.deal(AfterSaleDetailActivity.this, response, new TypeToken<BaseBean<AfterSaleBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(AfterSaleDetailActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<AfterSaleBean> beans) {
        if (beans.isEmpty()) {
            return;
        }
        AfterSaleBean bean = beans.get(0);
        Glide.with(this).load(bean.getGoods_thumb()).error(R.mipmap.error_small).into(imageView);
        nameTv.setText(bean.getGoods_name());
        String price = bean.getShop_price();
        orderNumTv.setText("订单编号：" + bean.getOrder_sn());
        statusTv.setText(bean.getStatus());
        goodsMoneyTv.setText(getString(R.string.rmb) + price);
        contentTv.setText(bean.getContent());
        List<String> imgs=bean.getImgArray();
        mAdapter.addAll(bean.getImgArray());
        recyclerView.refresh();
    }

    private void initRecy() {
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mAdapter = new GridImgAdapter(AfterSaleDetailActivity.this);
        mAdapter.setDataList(new ArrayList<String>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.refresh();

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setLoadMoreEnabled(false);

        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
//        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(AfterSaleDetailActivity.this,BrowseActivity.class);
                i.putExtra("path",mAdapter.getDataList().get(position));
                startActivity(i);
            }
        });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }
}
